package me.raih.controller.user;

import me.raih.pojo.po.User;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.MailService;
import me.raih.service.UserService;
import me.raih.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author Raih
 * @Time 2021/2/9  8:38
 *
 * 用户注册控制器
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserRegisterController {

    @Autowired
    UserService userService;

    @Autowired
    MailService mailService;

    @Autowired
    ResultInfo resultInfo;

    @RequestMapping(value = "/register")
    public String toRegister(){

        return "user/register";
    }

    @ResponseBody
    @RequestMapping(value = "/userRegisterCheck",method = RequestMethod.PUT)
    public ResultInfo registerCheck(@RequestParam("username")String username,
                                    @RequestParam("password")String password,
                                    @RequestParam("name")String name,
                                    @RequestParam("email")String email,
                                    @RequestParam("phone")long phone,
                                    @RequestParam("qq")int qq,
                                    @RequestParam("address")String address,
                                    @RequestParam("securityQuestion")String securityQuestion,
                                    @RequestParam("answer")String answer
                                ){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setEmail(email);
        user.setPhone(phone);
        user.setAddress(address);
        user.setQq(qq);
        user.setSecurityQuestion(securityQuestion);
        user.setAnswer(answer);
        user.setStatus("N");
        String checkCode = UUIDUtil.bigString();
        user.setCheckCode(checkCode);
        boolean flag = userService.registerUser(user);

        if(flag){
            //若能注册才发送邮件
            if(email != null){
                boolean flag2 = mailService.sendSimEmail(username,email,checkCode);
                //如果能够发送成功邮件
                if(flag2){
                    resultInfo.setMessage("注册成功，请进入邮箱激活");
                }else {
                    resultInfo.setMessage("邮件注册失败！");
                }
                resultInfo.setFlag(flag2);
            }
        }
        else {
            resultInfo.setMessage("用户名已存在，请重试");
            resultInfo.setFlag(false);
        }
        return resultInfo;
    }

    @RequestMapping(value = "/emailCheck")
    @ResponseBody
    public String emailCheck(String username,String checkCode){

        boolean flag = userService.changeUserStatus(username,checkCode);
        if(flag){
            //成功激活
            return "激活成功，请自行进入首页";
        }else {
            return "激活失败！原因保密";
        }
    }

}
